/*
 * @Description: 接口
 * @Author: ziwei.ma
 * @Date: 2019-10-14 15:54:09
 * @Last Modified by: ziwei.ma
 * @Last Modified time: 2019-10-30 23:52:38
 */

import { UserBeanUtils, ajaxAsync } from 'vue-xiaobu-utils'
import AjaxForBusOpen from '@/utils/AjaxForBusOpen'

/**
 *
 * @param {number} phone 手机号
 * 获取验证码
 */
async function getVerificationCode(phone) {
  let res = await ajaxAsync('/user/smscode/get', { PHONE: phone })
  return res
}

/**
 *  登录的接口
 * @param {*} phone 手机号
 * @param {*} msgCode 验证码
 */
async function Login(phone, msgCode) {
  let json = {
    PHONE: phone,
    SMS_CODE: msgCode,
    WX_CODE: window.localStorage.getItem('wxCode') || undefined,
    CID: 'db92091a0a7057df248fe909d754e4e0'
  }
  let res = await ajaxAsync('/user/smscode/login/jiadx', json)
  localStorage.setItem('XIAOBUSESSION', res.SESSION_ID)
  res.PHONE = phone
  res.IS_LOGIN = 1
  UserBeanUtils.setUserInfo(res)
}

/**
 * 拉起微信JSPAY支付
 * @param {*} pretendOrderId 订单号
 */
async function payOrder(pretendOrderId) {
  let json = {
    PAYMENT: 'WXJSPAY',
    PRETEND_ORDER_ID: pretendOrderId
  }
  let res = await AjaxForBusOpen('order/add', json)
  return res
}

/**
 *
 * 绑定微信openid到用户----only 嘉定
 */
async function bindOpenID() {
  let res = await ajaxAsync('/user/bind/open_id', {})
  return res
}

/**
 *
 * 用户微信公众号绑定openid校验----only 嘉定
 */
async function verifyOpenID() {
  let res = await ajaxAsync('/user/wxjs/login/verify', {}, 1)// autoExLvl：自动处理异常等级,0表示都处理异常，1表示只自动处理协议网络异常，2表示不自动处理异常
  return res
}

/**
 * 退票
 * @param {*} ticketTypeID 票ID
 */
async function getDishonour(ticketTypeID) {
  let json = {
    TICKET_TYPE_ID: ticketTypeID
  }
  let res = await AjaxForBusOpen('ticket/refund', json)
  return res
}

/**
 * 获取地图拐点
 * @param {*} direction 上下行
 * @param {*} routeID 线路ID
 */
async function getLnglat(direction, routeID) {
  let json = {
    DIRECTION: direction,
    ROUTE_ID: routeID
  }
  let res = await AjaxForBusOpen('route/mapStationName', json)
  return res
}

/**
 * 获取订单状态
 * @param {*} orderNo 订单号
 */
async function getPurchaseResult(orderNO) {
  let json = {
    ORDER_NO: orderNO
  }
  let res = await AjaxForBusOpen('order/state', json)
  let resultObj = {
    state: res.ORDER_STATE,
    orderNO: res.ORDER_NO,
    hasBackTrack: res.HAS_BACK_TRACK
  }
  return resultObj
}

/**
 * 获取票状态，5秒刷一次
 * @param {*} ticketID 票ID
 */
async function getTicketSuccess(ticketID) {
  let json = {
    TICKET_ID: ticketID
  }
  let res = await AjaxForBusOpen('ticket/success_detail', json)
  return res.TICKET_FORWARD_STATE
}

/**
 * 用户手动发起核销车票
 * @param {*} ticketID 票ID
 */
async function userDestoryTicket(ticketID) {
  let json = {
    TICKET_ID: ticketID
  }
  let res = await AjaxForBusOpen('ticket/user/ticket_destroy', json, 1)// 自动处理异常等级,0表示都处理异常，1表示只自动处理协议网络异常，2表示不自动处理异常
  return res.TICKET_FORWARD_STATE
}

export {
  getVerificationCode,
  Login,
  payOrder,
  bindOpenID,
  verifyOpenID,
  getDishonour,
  getLnglat,
  getPurchaseResult,
  getTicketSuccess,
  userDestoryTicket
}
